<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include.html :: header('用户列表')"/>
    <style>
        .down {
            color: #1E9FFF;
            text-decoration: underline
        }

        div .base {
            font-size: 13px !important;
        }

        .blue_ {
            color: #0000FF;
            text-decoration: underline;
        }

        .blue_:hover {
            color: red;
        }
    </style>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-input-inline">
                    <input type="text" name="cond" placeholder=" 文 件 名 " class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" id="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                </button>
                <div class="pear-btn pear-btn-md" id="test1">
                    <i class="layui-icon layui-icon-upload"></i>
                    上传
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>


<script type="text/html" id="user-bar">
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i>
    </button>
</script>

<script type="text/html" id="pic-column">
    <div onclick="showImg(this)"><img src="{{d.productCover}}" alt="" title="点击放大" width="50px" height="34px"></div>
</script>

<script type="text/html" id="access-column">
    <a class="blue_" href="{{d.filePath}}" title="{{d.filePath}}" target="_blank">点击查看/访问</a>
</script>

<script type="text/html" id="status-column">
    {{# if(d.fileStatus == 1){ }}
    <button class="pear-btn pear-btn-primary pear-btn-xs base">使用中</button>
    {{# }else{ }}
    <button class="pear-btn pear-btn-warming pear-btn-xs base">未使用</button>
    {{# } }}
</script>
<script type="text/html" id="status2-column">
    {{# if(d.existStatus == 1){ }}
    <button class="pear-btn pear-btn-primary pear-btn-xs base">有</button>
    {{# }else{ }}
    <button class="pear-btn pear-btn-warming pear-btn-xs base">无</button>
    {{# } }}
</script>

<th:block th:include="include.html :: footer"/>
<script>

    /**
     * 点击展示正常尺寸图片
     * @param t
     */
    function showImg(t) {
        let $ = layui.jquery;

        let x = $(t).find("img");
        layer.open({
            type: 1,
            shadeClose: true,
            title: false,
            closeBtn: 0,
            end: function (index, layero) {
                return false;
            },
            content: '<div style="text-align: center"><img src="' + $(x).attr('src') + '"></div>'
        })
    }

    function showTip() {
        return layer.tips('<a href="/SteelPipe/goods/template" class="down">模板下载</a>', '#test2', {tips: [3, '#f2f2f2']});
    }


    layui.use(['table', 'form', 'jquery', 'drawer', 'dropdown', 'upload', 'popup'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let drawer = layui.drawer;
        let dropdown = layui.dropdown;
        let upload = layui.upload;
        let popup = layui.popup;


        // 导入
        let uploadInst = upload.render({
            elem: '#test1',
            url: suffix + '/common/upload',
            size: 5120,
            before: function () {
                layer.load(0, {shade: 0.3})
            },
            done: function (res) {
                layer.close(layer.load(0, {shade: 0.3}));
                if (res.code == 0) {
                    popup.success(res.msg, function () {
                        table.reload("user-table");
                    });
                } else {
                    popup.failure(res.msg);
                }
            },
            error: function (index, upload) {
                layer.close(layer.load(0, {shade: 0.3}));
                popup.failure("上传出错");
            }
        })

        let cols = [
            [
                // {title: '标题', field: 'productTitle', align: 'center'},
                {title: '原文件名', field: 'fileName', align: 'center'},
                {title: '现文件名', field: 'fileNowName', align: 'center'},
                {title: '文件大小/KB', field: 'fileSize', align: 'center'},
                {title: '文件位置', field: 'fileRealPath', align: 'center'},
                {title: '文件访问', field: 'filePath', align: 'center', templet: '#access-column'},
                {title: '使用状态', field: 'fileStatus', align: 'center', templet: '#status-column'},
                {title: '文件状态', field: 'existStatus', align: 'center', templet: '#status2-column'},
                // {title: '图片', field: 'newsTitle', align: 'center', templet: '#pic-column'},
                {title: '创建时间', field: 'createTime', align: 'center'},
                {title: '操作', toolbar: '#user-bar', align: 'center', width: 160, fixed: 'right'}
            ]
        ]

        table.render({
            elem: '#user-table',
            url: suffix + '/file/list',
            page: true,
            cols: cols,
            skin: 'line',
            defaultToolbar: ['', '', '']
        });

        table.on('tool(user-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        table.on('toolbar(user-table)', function (obj) {
            if (obj.event === 'import') {
                window.import();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            } else if (obj.event === 'importExcel') {
                window.importExcel();
            }
        });

        form.on('submit(user-query)', function (data) {
            table.reload('user-table', {
                where: {
                    cond: data.field.cond
                }
            })
            return false;
        });

        window.import = function () {
            // 不能删除
        }

        window.edit = function (obj) {
            console.log(obj)
            layer.open({
                type: 2,
                content: suffix + '/product/add?productId=' + obj.data.productId,
                area: ['940px', '95%'],
                title: '修改'
            });
        }

        window.remove = function (obj) {
            layer.confirm('确定删除', {
                icon: 3,
                shadeClose:true,
                title: '提示'
            }, function (index) {
                let loading = layer.load();
                $.ajax({
                    url: suffix + "/file/deleteOne",
                    dataType: 'json',
                    data: {fileId: obj.data['fileId'], existStatus: obj.data['existStatus']},
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                table.reload("user-table");
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.batchRemove = function (obj) {
            let data = table.checkStatus(obj.config.id).data;
            if (data.length === 0) {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }
            let ids = "";
            for (let i = 0; i < data.length; i++) {
                ids += data[i].goodId + ",";
            }
            ids = ids.substr(0, ids.length - 1);
            layer.confirm('确定删除', {
                icon: 3,
                shadeClose:true,
                title: '提示'
            }, function (index) {
                let loading = layer.load();
                $.ajax({
                    url: suffix + "/goods/deletes",
                    dataType: 'json',
                    data: {ids: ids},
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                table.reload('user-table');
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.refresh = function (param) {
            table.reload('user-table');
        }

        $("#reset").click(function () {
            $("input[name='cond']").val('');
            table.reload('user-table', {
                where: {
                    cond: ''
                }
            })
        });


    })
</script>
</body>
</html>

